﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CM_Classes;


namespace WPF_CM
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ServiceReference1.Service1Client sr1 = new ServiceReference1.Service1Client();
        Attrib attrib = new Attrib();

        public MainWindow()
        {
            

            InitializeComponent();
           
        }

       
      //User UI functionalities

        private void btnAddUser_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.Service1Client sr1 = new ServiceReference1.Service1Client();
            sr1.AddUser(Convert.ToInt32(txtEmpID.Text), txtFirstName.Text, txtLastName.Text, txtUserName.Text, txtPassword.Text, chbxUDisable.IsChecked.Value);
            dgdUsers.ItemsSource = sr1.GetUsers();
            MessageBox.Show("The record was added");
        }

        private void btnDeleteUser_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.Service1Client sr1 = new ServiceReference1.Service1Client();
            sr1.DeleteUser(Convert.ToInt32(txtEmpID.Text));
            dgdUsers.ItemsSource = sr1.GetUsers();
            MessageBox.Show("The record is deleted");
        }
        
        private void btnSaveUser_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.Service1Client sr1 = new ServiceReference1.Service1Client();
            sr1.SaveUser(Convert.ToInt32(txtEmpID.Text), txtFirstName.Text, txtLastName.Text, txtUserName.Text, txtPassword.Text, chbxUDisable.IsChecked.Value);
            dgdUsers.ItemsSource = sr1.GetUsers();
            MessageBox.Show("The record was updated.");
        }
        
        //Group UI functionalities

        private void btnAddGroup_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.Service1Client sr1 = new ServiceReference1.Service1Client();
            sr1.AddGroup(txtGroupName.Text, Convert.ToInt32(txtGroupPriority.Text), txtGroupDescription.Text, chbxGDisable.IsChecked.Value, txtSubGroupOf.Text);
            MessageBox.Show("The group has been added.");
        }

        private void btnSaveGroup_Click(object sender, RoutedEventArgs e)
        {
            Group group = new Group();
            ServiceReference1.Service1Client sr1 = new ServiceReference1.Service1Client();
            //sr1.SaveGroup(txtGroupName.Text, Convert.ToInt32(txtGroupPriority.Text), txtGroupDescription.Text, chbxGDisable.IsChecked.Value, txtSubGroupOf.Text);
            dgdGroups.ItemsSource = group.GetAllGroup();
            MessageBox.Show("The record was updated.");
        }

     

        // Attribute UI functionalities

        private void btnAddAttribute_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.Service1Client sr1 = new ServiceReference1.Service1Client();
            sr1.AddAttrib(txtAttributeName.Text, txtAttributeType.Text, chbxADisable.IsChecked.Value, txtAttributeDescription.Text);
            dgdAttributes.ItemsSource = attrib.GetAllAttrib();
            MessageBox.Show("The record is added");
       }

        private void btnSaveAttribute_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.Service1Client sr1 = new ServiceReference1.Service1Client();
            sr1.UpdateAttrib(Convert.ToInt32(txtAttributeID.Text), txtAttributeName.Text, txtAttributeType.Text, chbxADisable.IsChecked.Value, txtAttributeDescription.Text);
            dgdAttributes.ItemsSource = attrib.GetAllAttrib();
            MessageBox.Show("The record is saved/updated");
        }        

        private void btnDeleteAttribute_Click(object sender, RoutedEventArgs e)
        {
           

            ServiceReference1.Service1Client sr1 = new ServiceReference1.Service1Client();
            sr1.DeleteAttrib(Convert.ToInt32(txtAttributeID.Text));
            dgdAttributes.ItemsSource = attrib.GetAllAttrib();
            MessageBox.Show("The record is deleted");
        }


        private void btnAttributesearch_Click(object sender, RoutedEventArgs e)
        {
             sr1.SearchAttrib(txtAttributesearch.Text);

            txtAttributeID.Text = attrib.AttribID.ToString();
            txtAttributeName.Text = attrib.AttribName;
            txtAttributeType.Text = attrib.DataType;
            txtAttributeDescription.Text = attrib.AttribDescription;

        }

        private void btnAttributecancel_Click(object sender, RoutedEventArgs e)
        {

        }



        private void UserGroup_Loaded(object sender, RoutedEventArgs e)
        {
            dgdUserGroup.ItemsSource = sr1.GetUserGroups();
        }

        private void GroupAttrib_Loaded(object sender, RoutedEventArgs e)
        {
            GroupAttrib ga = new GroupAttrib();
            dgdGroupAttrib.ItemsSource = ga.GetGroupAttribs();
        }

        private void Attrib_Loaded(object sender, RoutedEventArgs e)
        {
            Attrib attrib = new Attrib();
            dgdAttributes.ItemsSource = attrib.GetAllAttrib();
        }

        private void Groups_Loaded(object sender, RoutedEventArgs e)
        {
            Group group = new Group();
            dgdGroups.ItemsSource = group.GetAllGroup();
        }

        private void User_Loaded(object sender, RoutedEventArgs e)
        {
            User user = new User();
            dgdUsers.ItemsSource = sr1.GetUsers();
        
        }

        private void dgdUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            User u = ((User)dgdUsers.SelectedItem);
            if (dgdUsers.SelectedIndex != -1)
            {
                txtEmpID.Text = u.EmpID.ToString();
                txtFirstName.Text = u.FirstName;
                txtLastName.Text = u.LastName;
                txtUserName.Text = u.Password;
                txtPassword.Text = u.Password;
                
            }
        }

        private void dgdGroups_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Group g = ((Group)dgdGroups.SelectedItem);
            if (dgdGroups.SelectedIndex != -1)
            {
                txtGroupID.Text = g.GroupID.ToString();
                txtGroupName.Text = g.GroupName;
                txtGroupPriority.Text = g.GroupPriority.ToString();
                txtGroupDescription.Text = g.GroupDescription;
                txtSubGroupOf.Text = g.SubGroupOf;

            }

        }

        private void dgdAttributes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Attrib a = ((Attrib)dgdAttributes.SelectedItem);
            if (dgdAttributes.SelectedIndex != -1)
            {
                txtAttributeID.Text = a.AttribID.ToString();
                txtAttributeName.Text = a.AttribName;
                txtAttributeType.Text = a.DataType.ToString();
                txtAttributeDescription.Text = a.AttribDescription;
                

            }

        }

       

        private void btnUsearch_Click(object sender, RoutedEventArgs e)
        {
            foreach (User u in dgdUsers.ItemsSource)
            {
                if (u.LastName == txtUsearch.Text)
                {
                    dgdUsers.SelectedItem = u;
                }
            }
            txtUsearch.Clear();
        }

       

        private void btnGroupsearch_Click(object sender, RoutedEventArgs e)
        {
            foreach (Group g in dgdGroups.ItemsSource)
            {
                if (g.GroupName == txtGroupSearch.Text)
                {
                    dgdGroups.SelectedItem = g;
                }
            }
            txtUsearch.Clear();

        }


      

      

       
        

       

      

       

       
    }
}
